package com.supor.aiot.basket.view;

import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.common.app.api.DownloadImpl;

public class HoriziontalRecycleView extends RecyclerView {

    private PointF mLastP;
    private RecycleScrollListener scrollListener;
    private boolean DOWN = false;
    public HoriziontalRecycleView(@NonNull Context context) {
        super(context);
    }

    public HoriziontalRecycleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HoriziontalRecycleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                if (mLastP!=null&&DOWN){
                    float distanceX = mLastP.x - ev.getRawX();
                    if (distanceX>200){
                        if (scrollListener!=null)scrollListener.onScrollLeft();
                    }else if (distanceX<-200){
                        if (scrollListener!=null)scrollListener.onScrollRight();
                    }
                }
                DOWN = false;
                break;
        }

        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                DOWN = true;
                if (mLastP == null) {
                    mLastP = new PointF();
                }
                mLastP.set(ev.getRawX(), ev.getRawY());
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }


    public void setHoriziontalScrollListener(RecycleScrollListener listener){
        this.scrollListener = listener;
    }


    public interface RecycleScrollListener{
        void onScrollLeft();
        void onScrollRight();
    }
}
